const path = require('path');
const express = require('express');
const logger = require('morgan');

const customerRouter = require( './routes/customer' );
const topicRouter = require( './routes/topic' );

const tokenHelper = require( './utils/token' );

const port = 3000 ;
const app = express();

app.use( logger('dev') );
app.use( express.json() );
app.use( express.urlencoded({ extended: false }) );
app.use( express.static(path.join(__dirname, 'public')) );

const unlessPaths = [ 
    '/customer/sign/in',
    '/customer/sign/up' ,
    '/topic/list',
    '/topic/search'
];
const jwtMiddleware = tokenHelper.middleware( unlessPaths );
// 使用 express-jwt 提供的中间件支持解析 Token
// 并将 Token 中的信息保存到 req 的 user 属性中
app.use( jwtMiddleware );

app.use( customerRouter );
app.use( topicRouter );

app.listen( port );